// 首页数据上报，分析用户活跃度
function analytics(){
    //数据上报
    //image对象发送上报请求
    // let image = new Image(1,1)
    // image.src = "http://47.106.83.33/upload.gif?uid=ssfsf-42221&date="+Date.now()
    let info;
    info = getInfo();
    batchSend(info);
}

// 商品详情页面，数据上报埋点
function analyticsItemDetail(itemId){
    let clickData = {dom_id: "button-6-8",
        page: "info|6|"+itemId,
        platform: "h5",
        event_name: "viewProductDetail",
        type: "button"};
    console.log(clickData);
    info = getInfo(clickData);
    batchSend(info);
}

// 商品结算页面，数据上报埋点
function analyticsItemOrder(orderItemList){
    for(let i=0;i<orderItemList.length;i++){
        let clickData = {dom_id:'button-7-7',
            page:'info|7|'+orderItemList[i].itemId,
            platform:'h5',
            event_name:'goPayProduct',
            type:'button'};
        console.log(clickData);
        info = getInfo(clickData);
        batchSend(info);
    }
}

// 商品结算页面，数据上报埋点
function analyticsSubmitOrder(orderItemList){
    for(let i=0;i<orderItemList.length;i++){
        let clickData = {dom_id:'button-5-7',
            page:'info|2|'+orderItemList[i].itemId,
            platform:'h5',
            event_name:'submitOrder',
            type:'button'};
        console.log(clickData);
        info = getInfo(clickData);
        batchSend(info);
    }
}
// 按钮点击事件绑定，提供数据上报
Vue.directive("click-stat",{
    bind(el,binding){
        el.addEventListener("click",()=>{
            let clickData = binding.value;
            console.log(clickData);
            let info;
            info = getInfo(clickData);
            batchSend(info);
        })
    }
})

// 数据上报
function send(uploadData){
    // let shopServerUrl = app.shopServerUrl;
    //fetch()发送上报请求
    let _data = Base64.encode(uploadData);
    let url = "http://http://47.106.83.33/upload.gif?"+ _data;
    fetch(
        url,
        {method: "head", mode: "no-cors"}
    )
}

//批量发送
function batchSend(infoData){
    let num = 3;
    let u_key = "u_data";
    let obj;
    let key = localStorage.getItem(u_key);
    if(key == null){
        obj = []
    }else{
        obj = JSON.parse(key);
    }

    if(infoData["uuid"]===null){
        return;
    }

    if(obj.length >= num){
        send(JSON.stringify(obj));
        localStorage.removeItem(u_key);
    }else{
        obj.push(infoData);
        localStorage.setItem(u_key,JSON.stringify(obj));
    }
}

//基础数据组装
function basicInfo(){
    let basicData = {};
    let uuid = null;
    let agent = null;

    uuid = getUUID();
    agent = getAgent();

    basicData['uuid'] = uuid;
    basicData['agent'] = agent;
    basicData['time'] = Date.now();
    return basicData;
}

//获取用户uid
function getUUID(){
    let uid = null;
    let c_key = "user";
    let user ={};
    let user_cookie = app.getCookie(c_key);
    if(user_cookie !== null && user_cookie !== undefined && user_cookie !== ''){
        let user_info_str = decodeURIComponent(user_cookie);
        if (user_info_str !== null && user_info_str !== undefined && user_info_str !== '') {
            let user_info = JSON.parse(user_info_str);
            if (typeof(user_info)  == "object" ) {
                user = user_info;
                uid = user.id;
            }
        }
    }
    return uid;
}

//获取客户端的操作系统
function getAgent(){
    let agent = null;
    let userAgent = navigator.userAgent;
    let agents =  ["Android", "iPhone","iPad","Mac"];
    let flag = true;
    for(let i=0;i<agents.length;i++){
        if(userAgent.indexOf(agents[i]) > 0){
            agent = agents[i];
            flag = false;
            break;
        }
    }
    if(flag){
        agent = "windows";
    }
    return agent;
}

//数据组装
function getInfo(extendData = null){
    let data;
    data = basicInfo();
    data['event'] = extendData;
    return data;
}

// export {getInfo,batchSend,analytics}

